package com.zou.lotterylucky.utils

import android.content.Context
import com.zou.lotterylucky.Constants
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

object ClockUtil {
//    fun getCurrentTime(context: Context): Long {
//        return System.currentTimeMillis()
////        val ifSynced = SPUtils.get(context, Constants.SP_TIME_SYNCED, false) as Boolean
////        return if (ifSynced) {
////            val diff = SPUtils.get(context, Constants.SP_TIME_Diff, 0L) as Long
////            SystemClock.elapsedRealtime() + diff
////        } else {
////            System.currentTimeMillis()
////        }
//    }


    fun ifSynced(context: Context): Boolean {
        return SPUtils.get(context, Constants.SP_TIME_SYNCED, false) as Boolean
    }

    val dateFormatter = SimpleDateFormat("dd-MM-yyyy")

    fun getDayEnd(context: Context) = SPUtils.get(context, Constants.SP_DAY_END, 1250) as Int

    fun getEndTime(context: Context, currentTime: Long): Long {
        val zeroTime = dateFormatter.parse(dateFormatter.format(currentTime))!!.time
//        XLog.e(dateFormatter.format(currentTime) + hourFormatter.format(currentTime))
//        XLog.e(dateFormatter.format(zeroTime) + hourFormatter.format(zeroTime))
        val dayEnd = getDayEnd(context)
        return if ((currentTime - zeroTime) >= (dayEnd * 60000))
            zeroTime + (dayEnd + 24 * 60) * 60000
        else zeroTime + dayEnd * 60000

//        val list = hourFormatter.format(currentTime).split(":")
//        val h = list[0].toInt()
//        val m = list[1].toInt()
//        val t = h * 60 + m
//        return currentTime - t * 60000 + dayEnd * 60000
    }

    fun isToday(createdAt: String): Boolean {
        val todayFormat = SimpleDateFormat("yyyy-MM-dd", Locale("nl", "NL")).apply {
            timeZone = TimeZone.getTimeZone("GMT-4")
        }
        if (createdAt.contains(todayFormat.format(Date()))) {
            return true
        }
        return false
    }


    @Throws(ParseException::class)
    fun UTCToCST(UTCStr: String?, format: String?) {
        var date: Date? = null
        val sdf = SimpleDateFormat(format)
        date = sdf.parse(UTCStr)
        println("UTC时间: $date")
        val calendar = Calendar.getInstance()
        calendar.time = date
        calendar[Calendar.HOUR] = calendar[Calendar.HOUR] + 8
        //calendar.getTime() 返回的是Date类型，也可以使用calendar.getTimeInMillis()获取时间戳
        System.out.println("北京时间: " + calendar.time)
    }
}